//
//  Yahoo_Search_Mobile.m
//  MacStockApp
//
//  Created by 英傑 翁 on 12/3/5.
//  Copyright (c) 2012年 翁英傑. All rights reserved.
//

#import "Yahoo_Search_Mobile.h"

@implementation Yahoo_Search_Mobile
- (id)init_identify:(NSString *)ident {
    
    if(self=[super init])
    {
        
        identify=ident;
        
        
    }
    
    return self;
}

-(void)dealloc{
    
    
    [super dealloc];
}
/*
http://finance.ipad.mobile.yahoo.com/2e76/v1/symbol;search=Aapl?format=json
X-Client-Info: vendor="Yahoo!"; model="iPad Finance"; version="1.2"\r\n
X-Request-Id: CF16D44A-178BB887\r\n
X-Proxy-Authorization: Digest nonce="Lhzd0Otl0PIUecspPPU2dqrLYNe85_AWd7597A--" uri="http://finance.ipad.mobile.yahoo.com/2e76/v1/symbol;search=A?format=json" response="V321FZK7ASkoWh3vkXdNQw--"\r\n
X-Client-UUID: F5074AD3-E202-4CAB-B29E-93B4CBE125DF\r\n
X-Device-Udid: 17243a4e72c7eadd0e9823cd887f97212efa43fa\r\n
X-Device-Info: id="6693"; make="Apple"; model="iPad"; os="iPhone OS"; osver="5.0.1"\r\n
*/

-(NSMutableArray *)get_search_array_data:(NSString *)serachstrings {
    
    NSString *urls=[NSString stringWithFormat: @"http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=%@&callback=YAHOO.Finance.SymbolSuggest.ssCallback",serachstrings] ;      
    
    
    NSURL *  urla;
    urla = [NSURL URLWithString:urls];
    
    NSError * errorq = nil;
    NSString * download_string;
    download_string=[NSString stringWithContentsOfURL:urla encoding:NSUTF8StringEncoding error:&errorq];
    
    
    //去除無用的字串 '//'
    NSString *string_for_jsonvalue= [[download_string stringByReplacingOccurrencesOfString: @"YAHOO.Finance.SymbolSuggest.ssCallback(" withString:@"" ] stringByReplacingOccurrencesOfString:@")" withString:@""];
    
    //NSLog(@"string_for_jsonvalue=%@", string_for_jsonvalue);
    
    NSDictionary *jsonvalue_elements = [[[string_for_jsonvalue JSONValue] objectForKey:@"ResultSet"] objectForKey:@"Result"];
    
    NSMutableArray *outarray=[[[NSMutableArray alloc] init] autorelease];
    
    for (NSDictionary *sss in jsonvalue_elements ) {
        
        NSMutableDictionary *temdic=[[[NSMutableDictionary alloc] init] autorelease];
        
        NSString *code_s=@"";         //全名
        NSString *symbol_s=@"";       //代碼
        NSString *country_s=@"";      //交易所代碼  
        NSString *name_s=@"";         //名稱
        NSString *typeDisp_s=@"";     //標的種類
        NSString *exchDisp_s=@"";     //交易所名稱
        NSString *class_s=@"na."; //yahoo 資料不提供證券產業類別
        
        //分解出資料
        if ([sss objectForKey:@"symbol"]!=nil) {
            
            code_s=[sss objectForKey:@"symbol"];            
            
            //將字串分割
            NSArray * Timestamp_array=[[sss objectForKey:@"symbol"]  componentsSeparatedByString:@"."]; 
            if ([Timestamp_array count]==2) {
                symbol_s=[Timestamp_array objectAtIndex:0];
                country_s=[Timestamp_array objectAtIndex:1];
            }            
            if ([Timestamp_array count]==1) {
                symbol_s=[Timestamp_array objectAtIndex:0];
                country_s=@"";
            }             
            
        }
        else {
            symbol_s=@"na";
            country_s=@"na";
            code_s=@"";
        }   
        
        if ([sss objectForKey:@"name"]!=nil) {
            name_s=[sss objectForKey:@"name"];
            
        }
        else {
            name_s=@"na";
        }           
        
        if ([sss objectForKey:@"typeDisp"]!=nil) {
            
            typeDisp_s=[sss objectForKey:@"typeDisp"] ;
        }
        else {
            typeDisp_s=@"na";
        }  
        
        if ([sss objectForKey:@"exchDisp"]!=nil) {
            
            exchDisp_s=[sss objectForKey:@"exchDisp"];
            
        }
        else {
            exchDisp_s=@"na";
        }            
        
        
        [temdic setObject:code_s forKey:@"code"];
        [temdic setObject:symbol_s forKey:@"symbol"];
        [temdic setObject:country_s forKey:@"country"];        
        [temdic setObject:name_s forKey:@"name"];
        [temdic setObject:typeDisp_s forKey:@"typeDisp"];
        [temdic setObject:exchDisp_s forKey:@"exchDisp"];        
        [temdic setObject:class_s forKey:@"class"];        
        
        [outarray addObject:temdic];
        
        
    }        
    
    return outarray;    
}

@end
